package co.yixiang.hw.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.hw.domain.YxUserAddress;
import co.yixiang.hw.service.YxUserAddressService;
import co.yixiang.hw.service.dto.YxStoreOrderDto;
import co.yixiang.hw.service.dto.YxUserAddressDto;
import co.yixiang.hw.service.dto.YxUserAddressQueryCriteria;
import co.yixiang.hw.vo.YxUserAddressVo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author Walert
* @date 2020-02-18
*/
@Api(tags = "用户收货地址接口API管理")
@RestController
@RequestMapping("/api/yxUserAddress")
public class YxUserAddressController extends BaseController {

    private final YxUserAddressService yxUserAddressService;

    public YxUserAddressController(YxUserAddressService yxUserAddressService) {
        this.yxUserAddressService = yxUserAddressService;
    }

    @Log("查询用户收货地址接口API")
    @ApiOperation(value = "查询用户收货地址接口API",response = YxUserAddressDto.class)
    @PostMapping(value = "/list")
//    @AnonymousAccess
    public ResponseEntity<Object> getYxUserAddresss(@RequestBody JSONObject params){
        return new ResponseEntity<>(yxUserAddressService.findAllByUid(getUserId().intValue()),HttpStatus.OK);
    }

    @Log("新增用户收货地址接口API")
    @ApiOperation(value = "新增用户收货地址接口API",response = YxUserAddressDto.class)
    @PostMapping(value = "/add")
    public ResponseEntity<Object> create(@Validated @RequestBody YxUserAddressVo resources){
        if(resources.getIsDefault() == 1){
            yxUserAddressService.settingNoDefaultByUid(getUserId().intValue());
        }
        Long addTime = System.currentTimeMillis() /1000L;
        YxUserAddress userAddress = new YxUserAddress();
        userAddress.setCity(resources.getCity());
        userAddress.setPhone(resources.getPhone());
        userAddress.setProvince(resources.getProvince());
        userAddress.setDetail(resources.getDetail());
        userAddress.setDistrict(resources.getDistrict());
        userAddress.setPostCode("0");
        userAddress.setIsDefault(resources.getIsDefault());
        userAddress.setRealName(resources.getRealName());

        userAddress.setAddTime(addTime.intValue());
        userAddress.setUid(getUserId().intValue());
        userAddress.setIsDel(0);
        userAddress.setLatitude("0");
        userAddress.setLongitude("0");

        return new ResponseEntity<>(yxUserAddressService.create(userAddress),HttpStatus.OK);
    }

    @Log("修改用户收货地址接口API")
    @ApiOperation(value = "修改用户收货地址接口API",response = YxUserAddressDto.class)
    @PostMapping(value = "/update")
    public ResponseEntity<Object> update(@RequestBody YxUserAddressVo resources){

        if(resources.getIsDefault() == 1){
            yxUserAddressService.settingNoDefaultByUid(getUserId().intValue());
        }
        YxUserAddress userAddress = new YxUserAddress();
        userAddress.setId(resources.getId());
        userAddress.setCity(resources.getCity());
        userAddress.setPhone(resources.getPhone());
        userAddress.setProvince(resources.getProvince());
        userAddress.setDetail(resources.getDetail());
        userAddress.setDistrict(resources.getDistrict());
        userAddress.setIsDefault(resources.getIsDefault());
        userAddress.setRealName(resources.getRealName());
        yxUserAddressService.update(userAddress);
        return new ResponseEntity<>(yxUserAddressService.findById(resources.getId()),HttpStatus.OK);
    }

    @Log("删除用户收货地址接口API")
    @ApiOperation("删除用户收货地址接口API")
    @PostMapping(value = "/delete")
    public ResponseEntity<Object> deleteAll(@RequestBody JSONObject params) {
        JSONArray ids = params.getJSONArray("ids");
        yxUserAddressService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


}